package org.wu.framework.authorization.platform.application.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.wu.framework.authorization.platform.application.SysRoleApplication;
import org.wu.framework.authorization.platform.application.assembler.SysRoleDTOAssembler;
import org.wu.framework.authorization.platform.application.command.SysRoleCommand;
import org.wu.framework.authorization.platform.domain.model.sys.role.SysRole;
import org.wu.framework.authorization.platform.domain.model.sys.role.SysRoleRepository;
import org.wu.framework.authorization.platform.domain.model.sys.role.menu.SysRoleMenu;
import org.wu.framework.authorization.platform.domain.model.sys.role.menu.SysRoleMenuRepository;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermission;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermissionRepository;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl;
import org.wu.framework.lazy.orm.web.plus.stereotype.LazyApplication;
import org.wu.framework.web.response.Result;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe 角色
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyApplicationImpl
 **/
@LazyApplication
public class SysRoleApplicationImpl implements SysRoleApplication {

    @Autowired
    SysRoleRepository sysRoleRepository;

    @Autowired
    SysRoleMenuRepository sysRoleMenuRepository;

    @Autowired
    SysRolePermissionRepository sysRolePermissionRepository;

    /**
     * describe 新增角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRole> save(SysRoleCommand sysRoleCommand) {
        SysRole sysRole = SysRoleDTOAssembler.toRole(sysRoleCommand);
        sysRole.setIsDeleted(false);
        Result<SysRole> roleResult = sysRoleRepository.story(sysRole);

        SysRole data = roleResult.getData();
        Long roleId = data.getId();
        List<SysRoleMenu> sysRoleMenuList = sysRoleCommand.getMenuIds().stream().map(menuId -> {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setIsDeleted(false);
            sysRoleMenu.setRoleId(roleId);
            return sysRoleMenu;
        }).collect(Collectors.toList());

        sysRoleMenuRepository.removeRoleMenus(sysRoleCommand.getId());
        sysRoleMenuRepository.batchStory(sysRoleMenuList);


        List<SysRolePermission> sysRolePermissionList = sysRoleCommand.getPermissionIds().stream().map(permissionId -> {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setPermissionId(permissionId);
            sysRolePermission.setIsDeleted(false);
            sysRolePermission.setRoleId(roleId);
            return sysRolePermission;
        }).collect(Collectors.toList());
        sysRolePermissionRepository.removeRolePermission(sysRoleCommand.getId());
        sysRolePermissionRepository.batchStory(sysRolePermissionList);
        return roleResult;
    }

    /**
     * describe 更新角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRole> update(SysRoleCommand sysRoleCommand) {
        return save(sysRoleCommand);
    }

    /**
     * describe 查询单个角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRole> findOne(SysRoleCommand sysRoleCommand) {
        SysRole sysRole = SysRoleDTOAssembler.toRole(sysRoleCommand);
        return sysRoleRepository.findOne(sysRole);
    }

    /**
     * describe 查询多个角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<List<SysRole>> findList(SysRoleCommand sysRoleCommand) {
        SysRole sysRole = SysRoleDTOAssembler.toRole(sysRoleCommand);
        return sysRoleRepository.findList(sysRole);
    }

    /**
     * describe 删除角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRole> delete(SysRoleCommand sysRoleCommand) {
        SysRole sysRole = SysRoleDTOAssembler.toRole(sysRoleCommand);
        return sysRoleRepository.delete(sysRole);
    }

}